<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="css/test.css">
<%@ page import="java.util.*" %>
<title>Administor</title>
<script type="text/javascript">
var xmlHttp;
function create()
{
	var td = document.createElement("table");
	tr = td.createElement("tr");
	tr.createElement("td");
}
function deleteData(obj)
{
	var username = obj.parentElement.parentElement.cells[0].getElementsByTagName("input")[0].value;
	var proc = "delete";
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlHttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	content = "username="+encodeURIComponent(username.toString())+"&proc="+encodeURIComponent(proc.toString());
	xmlHttp.open("POST", "DataBaseProcesseSvr", false);
	xmlHttp.onreadystatechange = callback;
	xmlHttp.setRequestHeader("Content-Length",content.length);
	xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
	xmlHttp.send(content);
	
}

function modifyData(obj)
{
	var username = obj.parentElement.parentElement.cells[0].getElementsByTagName("input")[0].value;
	var password = obj.parentElement.parentElement.cells[1].getElementsByTagName("input")[0].value;
	var proc = "modify";
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlHttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	content = "username="+encodeURIComponent(username.toString())+"&proc="+encodeURIComponent(proc.toString())+"&password="+encodeURIComponent(password.toString());
	xmlHttp.open("POST", "DataBaseProcesseSvr", false);
	xmlHttp.onreadystatechange = callback;
	xmlHttp.setRequestHeader("Content-Length",content.length);
	xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
	xmlHttp.send(content);

}

function callback()
{
	if(xmlHttp.readyState == 4) 
	{
	   if(xmlHttp.status == 200) 
	   {
			history.go(0);
	   }
	}
}
function addData(obj)
{
	var username = obj.parentElement.parentElement.cells[0].getElementsByTagName("input")[0].value;
	var password = obj.parentElement.parentElement.cells[1].getElementsByTagName("input")[0].value;
	var proc = "add";
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlHttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	content = "username="+encodeURIComponent(username.toString())+"&proc="+encodeURIComponent(proc.toString())+"&password="+encodeURIComponent(password.toString());
	xmlHttp.open("POST", "DataBaseProcesseSvr", false);
	xmlHttp.onreadystatechange = callback;
	xmlHttp.setRequestHeader("Content-Length",content.length);
	xmlHttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded");
	xmlHttp.send(content);
}

function addRow()
{
    var table = document.getElementById("content");  
    var row = table.insertRow(table.rows.length);
    var cell1 = row.insertCell(0);
    var cell2 = row.insertCell(1);
    var cell3 = row.insertCell(2);
    var cell4 = row.insertCell(3);
    cell1.innerHTML="<input type='text' name='username'>";  
    cell2.innerHTML="<input type='text' name='password'>"; 
    cell3.innerHTML="<input type='button' name='delete' onclick='addData(this)' value='增加'>"; 
    cell4.innerHTML="<input type='button' name='modify' onclick='modifyData(this)' value='修改'>"; 

}

</script>
</head>
<body>

<%! String currentUser = null; %>
<%
//判断是否已经有用户登录
	currentUser = (String)session.getAttribute("currentUser");
	if(null != currentUser)
	{
		if("administor".equals(currentUser))
		{
			out.print("<p id='welcomeInfo'> 当前管理员用户："+currentUser+" <a href='/ImageTest/test.jsp'>退出当前用户</a></p>");
		}else
		{
			out.print("<p text-align='center' font-size=20px> 您好，您不是系统管理员！请先 <a href='/ImageTest/login.jsp'>登录管理员</a></p>");
			return;
		}
	}else
	{
		out.print("<p text-align='center' font-size=20px> 您好 请先 <a href='/ImageTest/login.jsp'>登录</a></p>");
		return;
	}
%>


<div id="header"></div>
<ul id="nav">
<li><a href="/ImageTest/index.jsp">首页</a></li>
<li><a href="/ImageTest/viewImage.jsp">看图片</a></li>
<li><a href="/ImageTest/upload_file.jsp">上传图片</a></li>
<li><a href="/ImageTest/login.jsp">登录</a></li>
<li><a href="/ImageTest/regist.jsp">注册用户</a></li>
<li><a href="/ImageTest/administor.jsp">管理员</a></li>
</ul>


<%!List alluser; %>
<% 
alluser = (List)request.getAttribute("alluserinfo");
if(null != alluser)
{
	int size = alluser.size();
	//out.println("username"+"&emsp;"+"password"+"<br>");
	out.print("<table id='content'>");
	out.print("<tr><th>UserName</th><th>Password</th></tr>");
	for(int i =0; i < size; i++)
	{
		List tmp = (List)alluser.get(i);
		for(int j =0; j<2; j++)
		{
			if(j == 0)
			{
				String name="username"+Integer.valueOf(i).toString()+Integer.valueOf(j).toString();
				//System.out.println(name);
				out.print("<tr><td><input type='text' name="+name+" value="+tmp.get(j)+" readonly='readonly'></td>");
			}else
			{
				String password="password"+Integer.valueOf(i).toString()+Integer.valueOf(j).toString();
				//System.out.println(password);
				out.print("<td><input type='text' name="+password+" value="+tmp.get(j)+"><td id='delete'><input type='button' onclick='deleteData(this)' name='delete' value='删除'><td id='modify'><input type='button' name='modify' onclick='modifyData(this)' value='修改'></td></td></tr>");
			}
		}
	}
	out.print("</table>");
}
	
%>

<form action="databaseinfo" method="post">
<input type="submit" onclick="create()" value="刷新" >
</form>
<input type='submit' onclick='addRow()' value='添加数据' >
</body>
</html>
